/**
 * Created by liubin on 2014/07/03.
 */
'use strict';
/**
 * 服务初始化
 */
define(['angular'], function (angular) {
    return angular.module('agcompany.services', ['ngResource']).
        factory('alertService', function () {
            return function ($scope) {
                $scope.alerts = [];
                $scope.addAlert = function (type, value) {
                    $scope.alerts.push({type: type, msg: value});
                };
                // 关闭所有错误框
                $scope.closeAlert = function (index) {
                    $scope.alerts.splice(index, 1);
                };
                // 关闭单个错误框
                $scope.closeAllAlert = function () {
                    $scope.alerts.splice(0, $scope.alerts.length);
                };
            }
        }
    ).service('baseService', ['$resource', function ($resource) {
            this.process = function (sev, funcName, args, posts, successFunc, failtureFunc, errorFunc) {
                var hasError = false;
                sev[funcName](args,
                    posts,
                    function (success) {
                        console.log(success.data);
                        if (successFunc) {
                            successFunc(success);

                        }
                    },
                    function (error) {
                        console.log('service failure');
                        hasError = true;
                        console.log(error);
                        if (failtureFunc) {
                            failtureFunc(error);

                        }
                    }).$promise.catch(
                    function (error) {
                        if (!hasError) {
                            console.log('service $promise catch');
                            console.log(error);
                            if (errorFunc) {
                                errorFunc(error);
                            }
                        }
                    }
                );
            }
        }]);
});